package handler

import (
	"github.com/gin-gonic/gin"
	"media-hub/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	data := model.Article{
		Title:   title,
		Content: content,
		Img:     img,
	}
	err := model.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
	}
}

func List(c *gin.Context) {
	var article []model.Article
	err := model.DB.Debug().Find(&article).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "展示失败",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "展示成功",
			"data": article,
		})
	}
}
